package com.shiroha.account.config.security.keycloak;

import com.shiroha.account.service.IAccountService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class KeycloakOAuth2LogoutHandler implements LogoutHandler{
    private final IAccountService accountService;

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication){
        OidcUser user = (OidcUser) authentication.getPrincipal();
        String userId = user.getClaimAsString("sub");
        accountService.logout(userId);
    }
}
